Skip to content

Conversation

@vinceAmstoutz
Copy link
Member

Q A
Branch? 3.4
Tickets Closes #6682
License MIT

As mentioned in #6682, QueryParamter on DTO with stateOptions doesn't work. This PR will solve this problem by applying stateOptions to the ParameterResourceMetadataCollectionFactory.

@vinceAmstoutz vinceAmstoutz marked this pull request as draft October 15, 2024 14:25
@vinceAmstoutz vinceAmstoutz force-pushed the fix-query-parameter-on-dto-with-state-options branch 2 times, most recently from a2c48a9 to 11cc1e5 Compare October 15, 2024 14:44
@vinceAmstoutz vinceAmstoutz force-pushed the fix-query-parameter-on-dto-with-state-options branch from 11cc1e5 to 0affd52 Compare October 15, 2024 18:56
Replaced state options logic in `ParameterResourceMetadataCollectionFactory` with a new method `getClassFromStateOptions`. This simplifies the code by centralizing the logic in the `Metadata` class and improves readability.
@vinceAmstoutz vinceAmstoutz force-pushed the fix-query-parameter-on-dto-with-state-options branch from 7f2128d to 55f4c94 Compare October 15, 2024 19:14
@vinceAmstoutz vinceAmstoutz force-pushed the fix-query-parameter-on-dto-with-state-options branch from 1fedd26 to 1fd61cb Compare October 15, 2024 19:22
@soyuka soyuka force-pushed the fix-query-parameter-on-dto-with-state-options branch from 20f4b70 to c605174 Compare October 16, 2024 07:42
@vinceAmstoutz vinceAmstoutz marked this pull request as ready for review October 16, 2024 07:51
@soyuka soyuka changed the title fix(QueryParameter): try to improve handling of stateOptions fix: try to improve handling of stateOptions Oct 16, 2024
@soyuka soyuka changed the title fix: try to improve handling of stateOptions fix: use stateOptions when retrieving a Parameter Doctrine filter Oct 16, 2024
@soyuka soyuka force-pushed the fix-query-parameter-on-dto-with-state-options branch from c605174 to fe4bce4 Compare October 16, 2024 08:16
@soyuka soyuka changed the title fix: use stateOptions when retrieving a Parameter Doctrine filter fix: use stateOptions when retrieving a Parameter filter Oct 16, 2024
@soyuka soyuka merged commit d34cd7b into api-platform:3.4 Oct 16, 2024
@bendavies
Copy link
Contributor

bendavies commented Oct 20, 2024

this doesn't seem to fully work from what i can see:

the hydra:template looks correct and has foo[before],foo[strictly_before],foo[after],foo[strictly_after] etc...
but the swagger docs do not have 4 fields for before, strictly_before etc... do you know where this is done please @soyuka ?

@bendavies
Copy link
Contributor

bendavies commented Oct 20, 2024

ok, actually, if you try to use a different property name on your entity to your resource class, like the following, the hydra:template is then incorrect and will just have birthday with no [before] etc...

#[ORM\Entity]
class Agent
{
    #[ORM\Column]
    public ?\DateTimeImmutable $dob = null;

}
...

#[ApiFilter(DateFilter::class, properties: ['dob'], alias: 'app_filter_date')]
#[ApiResource(
    shortName: 'Agent',
    operations: [
        new GetCollection(parameters: [
            'birthday' => new QueryParameter(filter: 'app_filter_date'. property: 'dob'),
        ]),
    ],
    stateOptions: new Options(entityClass: Agent::class)
)]
class AgentApi
{
...
}

Querying this does work as expected however:
/api/agents?birthday[after]=2024-01-01

@soyuka
Copy link
Member

soyuka commented Oct 21, 2024

[before] is a value of our parameter though it should not appear in the template?

@bendavies
Copy link
Contributor

[before] is a value of our parameter though it should not appear in the template?

yes, it should appear in the template as per this test:

'hydra:template' => \sprintf('/%s{?foo,fooAlias,order[order[id]],order[order[foo]],searchPartial[foo],searchExact[foo],searchOnTextAndDate[foo],searchOnTextAndDate[createdAt][before],searchOnTextAndDate[createdAt][strictly_before],searchOnTextAndDate[createdAt][after],searchOnTextAndDate[createdAt][strictly_after],q,id,createdAt}', $route),

@vinceAmstoutz vinceAmstoutz deleted the fix-query-parameter-on-dto-with-state-options branch October 22, 2024 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants